package com.android.videotest.music.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class Utils {

    public static final String MUSIC_PLAY = "music_play";
    public static final String MEDIA_ID = "media_id";
    public static final String PROGRESS = "progress";

    public static void saveLastMusic(Context context, String mediaId) {
        SharedPreferences spn = context.getSharedPreferences(MUSIC_PLAY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spn.edit();
        editor.putString(MEDIA_ID, mediaId).commit();
    }

    public static String getLastMusic(Context context) {
        SharedPreferences spn = context.getSharedPreferences(MUSIC_PLAY, Context.MODE_PRIVATE);
        return spn.getString(MEDIA_ID, "");
    }

    /**
     * if url == null, clear all progress
     *
     * @param context context
     * @param url     if url!=null clear this url progress
     */
    public static void clearSavedProgress(Context context, Object url) {
        if (url == null) {
            SharedPreferences spn = context.getSharedPreferences(MUSIC_PLAY, Context.MODE_PRIVATE);
            spn.edit().clear().apply();
        } else {
            SharedPreferences spn = context.getSharedPreferences(MUSIC_PLAY, Context.MODE_PRIVATE);
            spn.edit().putLong("newVersion:" + url.toString(), 0).apply();
        }
    }

}
